/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.location;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Location;

public class LocationView extends Form implements CommandListener{
	private LocationController controller;
	
	//Commands
	private Command back;
	private Command nextLocation;
	private Command randomLocation;
	
	public LocationView(LocationController controller) {
		super("Location Provider");
		this.controller = controller;
		createCommands();
		this.setCommandListener(this);
	}
	
	private void createCommands(){
		nextLocation = new Command("next", "next location", Command.SCREEN, 0);
		randomLocation = new Command("random", "random location", Command.SCREEN, 0);
		this.addCommand(nextLocation);
		this.addCommand(randomLocation);
	}
	
	/**
	 * Initializes the form with the data of a Location
	 * @param l
	 */
	public void initialize(Location l){
		this.deleteAll();
		StringItem cellId = new StringItem("Cell-id: ", l.getCellid());
		StringItem lat = new StringItem("Latitude: ", l.getLat());
		StringItem lon = new StringItem("Longitude: ", l.getLon());
		
		this.append(cellId);
		this.append(lat);
		this.append(lon);
	}

	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.BACK){
			ScreenController.getInstance().last();
		}else if(c == nextLocation){
			controller.doNextLocation();
		}else if(c == randomLocation){
			controller.doRandomLocation();
		}
	}
}
